package com.xls.future.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.xls.future.R;
import com.xls.future.activity.ImageActivity;
import com.xls.future.entity.Photo;
import com.xls.future.listener.LoadImageListener;
import com.xls.future.utils.GlideApp;
import com.xls.future.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/28 0028.
 */

public class StaggeredAdapter extends RecyclerView.Adapter<StaggeredAdapter.ViewHolder> {



    private Activity context;
    private List<Photo> photos = new ArrayList<>();
    private RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.loading)
                .fallback(R.mipmap.failed)
                .error(R.mipmap.failed)
                .priority(Priority.HIGH);

    private Map<Integer,Integer> map = new HashMap<>();
    private LoadImageListener<Drawable> loadListener;

    public StaggeredAdapter(Activity context) {
        this.context = context;
        loadListener = new LoadImageListener<>();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_list, parent, false);
        return new ViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Photo photo = photos.get(position);
        holder.llRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("home","postion = "+position+" , url = "+photo.getImgUrl()+" | width = "+holder.ivPhoto.getLayoutParams().width+" | height ="+
                holder.ivPhoto.getLayoutParams().height +" | imgWidth = "+photo.getWidth()+" | imgHeight = "+photo.getHeight());
                Intent intent = new Intent(context, ImageActivity.class);
                intent.putExtra("url",photo.getImgUrl());
                if(Build.VERSION.SDK_INT>=21){
                    Log.d("xls","startActivity");
                    ActivityCompat.startActivity(context,intent, ActivityOptionsCompat.makeSceneTransitionAnimation(context,holder.ivPhoto,"image").toBundle());

                }else{
                    context.startActivity(intent);
                    context.overridePendingTransition(R.anim.scale_in, R.anim.scale_out);
                }

            }
        });

        int imgWidth = photo.getWidth();
        int imgHeight = photo.getHeight();
        int width = ScreenUtil.getScreenWidth(context)/2-ScreenUtil.dip2Px(12,context);
        int height = imgHeight*width/imgWidth;

        ViewGroup.LayoutParams lp = holder.ivPhoto.getLayoutParams();
        lp.width = width;
        lp.height = height;
        holder.ivPhoto.setLayoutParams(lp);
        GlideApp.with(context)
                .load(photo.getImgUrl())
                .override(lp.width,lp.height)
                .apply(options)
                .transition(DrawableTransitionOptions.withCrossFade())
                .listener(loadListener)
                .into(holder.ivPhoto);

    }

    @Override
    public int getItemCount() {
        return photos.size();
    }

    public void setData(List<Photo> photos) {
        this.photos = photos;
        notifyDataSetChanged();
    }

    public void addData(List<Photo> photos){
        int oldSize = this.photos.size();
        this.photos.addAll(photos);
        int newSize =  this.photos.size();
        for(int i= oldSize;i< newSize;i++){
            notifyItemInserted(i);
        }
    }

    public List<Photo> getPhotos() {
        return photos;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ll_root)
        LinearLayout llRoot;
        @BindView(R.id.iv_photo)
        ImageView ivPhoto;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
